package com.winchain.chainbill.receivables.biz.impt.rx;

import com.shell.bee.base.utils.DateUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.framework.utils.CommonDataUtil;
import com.winchain.chainbill.receivables.base.po.ArOperation;
import com.winchain.chainbill.receivables.base.po.ArRecord;
import com.winchain.chainbill.receivables.base.service.ArOptService;
import com.winchain.chainbill.receivables.base.service.ArRecordService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ReceivablesImptSubscriber extends OnErrorNotImplementedSubscriber<BatchReceivablesImptEvent> {
	@Autowired
	private ArRecordService arRecordService;
	@Autowired
	private ArOptService arOptService;

	public void onApplicationEvent(BatchReceivablesImptEvent t) {
		List<ArRecord> records = t.getRecs();

		ArOperation opt = t.getOperation();
		String bizId = "";
		String state = "";
		String nextState = opt.getNextState();
		int seqNo = 0;
		for (ArRecord record : records) {
			state = record.getArState();
			seqNo = record.getOptSeq().intValue();
			seqNo++;
			record.setArId(SystemUtil.getUUID());
			record.setOptSeq(Integer.valueOf(seqNo));
			record.setArState(nextState);
			this.arRecordService.save(record);
			bizId = record.getArId();

			opt.setOptId(SystemUtil.getUUID());
			opt.setBizId(bizId);
			opt.setState(state);
			opt.setNextState(nextState);
			opt.setOptTime(DateUtil.getSystemDateTime());
			opt.setOprId(CommonDataUtil.getUserId());
			opt.setSeqNo(Integer.valueOf(seqNo));
			this.arOptService.save(opt);
		}
	}
}
